(function(factory) {
	
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(["HteOS/desktop/View","HteOS/desktop/ContextMenu"],factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Metro 桌面组件
	 * @singleton
	 */
	HteOS.Desktop = {
	
		shorcutSize : 'small',

		/**
		 * @private
		 * 初始化桌面组件，由程序内部调用，请勿手工调用
		 */
		init: function() {
			
			this.shortcutSize = HteOS.Settings.shortcutSize || this.shortcutSize;
			
			var desktop =  $("<div class=\"hte-desktop\">").appendTo($(document.body));
			//初始化DOM元素
			$('<div class="hte-desktop-viewport hte-webkit-scrollbar">' + 
				'<div class="hte-desktop-container">' +
				 '</div>' + 
				'</div>').appendTo(desktop);
			if(HteOS.UA.Mobile){
				desktop.addClass("hte-desktop-mobile");
			}
			var logo = $("<div class=\"hte-desktop-logo\"><img  src=\""+HteOS.basePath+"images/logo.png\"> "+
						"<a href=\"#\"> <span class=\"segoe\">HteOS</span>"+
						"- <small>与众不同的 <span class=\"segoe\">Web</span>"+
					"</small>"+
					"</a><div>").appendTo(desktop);
			desktop.addClass("hte-desktop-" + this.shortcutSize);
			//$("<div class=\"hte-metro-group-nav\"></div>").appendTo(".hte-metro");

			var metro = this;
			var el = metro.getEl();
			
			// 如果发生浏览器改变大小，则自动调整位置
			$(window).resize(function() {
				metro.refresh();
			});
			
			HteOS.EventManager.on("hte.apps.instantiated",function(){
				metro.render();
			});
			
			//$('.hte-metro-container').perfectScrollbar();
			//$('.hte-metro-container').css("overflow","hidden");
			
			if(HteOS.UA.Mobile){
				//$('.hte-metro').width($(document.dody).width());
			}
			HteOS.desktop.ContextMenu.init();
			
			//点击桌面图标时执行应用
			$(document.body).on("click", ".hte-shortcut", function(event) {
				var app = HteOS.app.AppManager.getByEl($(this));
				HteOS.TaskManager.start(app);
				event.stopPropagation();
			});
			
		},
		
		setShortcutSize : function(shortcutSize){
			var container = this.getEl();
			container.removeClass("hte-desktop-" + this.shortcutSize);
			this.shortcutSize = shortcutSize;
			container.addClass("hte-desktop-" + this.shortcutSize);
			HteOS.EventManager.trigger("hte.desktop.shortcut.size.change",shortcutSize);
			//window.setTimeout(function(){	
				HteOS.desktop.View.refresh();
			//},210);
			
		},

		getEl: function() {
			return $(".hte-desktop");
		},
		
		render : function(){
			// 计算尺寸
			this.compute();
			HteOS.desktop.View.init();
			$(".hte-preloader").fadeOut(function(){
				HteOS.desktop.View.refresh();
			});
			//this.layout();
			//this.setSize();
			HteOS.EventManager.trigger("hte.desktop.render");
			this.rendered = true;
		},

		/**
		 * 计算桌面的尺寸
		 */
		compute: function() {
			var me = this;
			var containerHeight = $(document).height() - $(".hte-dockbar").height() - $(".hte-taskbar").height();
			me.getEl().find(".hte-desktop-container").height(containerHeight);
		},

		/**
		 * 刷新桌面图标排列
		 */
		refresh: function(group) {
			var el = this.getEl();
			this.compute();
			HteOS.desktop.View.refresh();
		}
	}
}));